package com.zhz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhz.domain.AdminBank;
import com.zhz.dto.AdminBankDto;
import com.zhz.feign.AdminBankServiceFeign;
import com.zhz.model.R;
import com.zhz.service.AdminBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author ：zhz
 * @date ：Created in 2021/01/01
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@RestController
@RequestMapping("/adminBanks")
@Api(tags = "公司银行卡的配置")
public class AdminBankController implements AdminBankServiceFeign {
    @Autowired
    private AdminBankService adminBankService;

    @GetMapping
    @ApiOperation(value = "条件查询公司银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankCard", value = "公司的银行卡"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('admin_bank_query')")
    public R<Page<AdminBank>> findByPage(@ApiIgnore Page<AdminBank> page, String bankCard) {
        return R.ok(adminBankService.findByPage(page, bankCard));
    }

    @PostMapping
    @ApiOperation(value = "新增公司银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "公司银行卡相关数据"),
    })
    @PreAuthorize("hasAuthority('admin_bank_create')")
    public R addAdminBank(@RequestBody @Validated AdminBank adminBank) {
        return adminBankService.save(adminBank) ? R.ok() : R.fail("新增失败");
    }

    @PostMapping
    @ApiOperation(value = "修改公司银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminBank", value = "公司银行卡相关数据"),
    })
    @PreAuthorize("hasAuthority('admin_bank_uodate')")
    public R updateAdminBank(@RequestBody @Validated AdminBank adminBank) {
        return  adminBankService.updateById(adminBank)?R.ok():R.fail("修改失败");
    }

    @PostMapping("/adminUpdateBankStatus")
    @ApiOperation(value = "禁用与启用(修改银行卡状态)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "受修改的银行卡的ID"),
            @ApiImplicitParam(name = "status", value = "修改状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_uodate')")
    public R changeStatus(Long bankId, Byte status) {
        AdminBank adminBank = new AdminBank();
        adminBank.setId(bankId);
        adminBank.setStatus(status);
        return adminBankService.updateById(adminBank)?R.ok():R.fail("状态修改失败");
    }

    @Override
    public List<AdminBankDto> getAllAdminBanks() {
        return adminBankService.getAllAdminBanks();
    }
}
